package client;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class MenuPanel extends JPanel{
    private static final long serialVersionUID = 1L;
    
    JButton lockButton;
    JButton logoutButton;
    
    JPanel menu;
    JPanel linePanel;
    Client client;
    
    public MenuPanel(Client client){
        this.client = client;
        
        // No layout
        this.setLayout(null);
        
        // Create elements
        linePanel = new JPanel();
        menu = new JPanel();
        lockButton = new JButton("Lock Computer");
        logoutButton = new JButton("Logout");
        
        // Setup buttons
        lockButton.setBounds(15, 15, 150, 25);
        logoutButton.setBounds(15, 45, 150, 25);
        
        // Setup menu
        menu.setBounds(2, 2, 196, 81);
        menu.setBorder(BorderFactory.createMatteBorder(15, 15, 15, 15, Color.white));
        menu.setBackground(Color.white);
        menu.setLayout(new GridLayout(2, 1, 5, 5));
        
        // Setup panel
        linePanel.setBounds(8, client.height-115, 180, 85);
        linePanel.setBorder(BorderFactory.createLineBorder(Color.gray, 2));
        
        // Assemble
        this.add(linePanel);        
        linePanel.add(menu, BorderLayout.CENTER);
        menu.add(lockButton);
        menu.add(logoutButton);
        
        // Add listeners
        this.addMouseListener(new MouseAction());
        lockButton.addActionListener(new LockAction());
        logoutButton.addActionListener(new LogoutAction());
    }
    
    private class LogoutExec implements Runnable{
        @Override
        public void run() {
            try {
                Thread.sleep(1700);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            client.removeBlackShowLogin();
        }
    }
    
    private class LogoutAction implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent arg0) {
            int option = JOptionPane.showConfirmDialog(client.frame, 
                    "Do you want to logout?",
                    "Logout", JOptionPane.YES_NO_OPTION,
                    JOptionPane.QUESTION_MESSAGE);
            if(option == 0){
                client.logout();
                new Thread(new LogoutExec()).start();
            }else{
                client.cancelLogout();
            }
            
        }
        
    }
    
    private class LockAction implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e) {
            client.lock();
        }
    }
    
    private class MouseAction implements MouseListener{
        @Override
        public void mouseClicked(MouseEvent arg0) {
            // TODO Auto-generated method stub
            
        }

        @Override
        public void mouseEntered(MouseEvent arg0) {
            // TODO Auto-generated method stub
            
        }

        @Override
        public void mouseExited(MouseEvent arg0) {
            // TODO Auto-generated method stub
            
        }

        @Override
        public void mousePressed(MouseEvent e) {
            Rectangle rect = new Rectangle(8, client.height-110, 180, 85);
            if(!rect.contains(e.getX(), e.getY())){
                client.hideMenu();
            }
        }

        @Override
        public void mouseReleased(MouseEvent arg0) {
            // TODO Auto-generated method stub
        }
    }

    public void paintComponent(Graphics g){}
}